package com.dudy.common.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: nacos_test
 * @author: dudy
 * @create: 2020-12-21 17:33
 * @description: 钉钉消息发送工具类
 **/
public class DingTalkUtil {

    // 要发送的钉钉地址
    private static String dingUrl = "";

    private static OkHttpClient clinet = new OkHttpClient();

    public static ExecutorService pool = new ThreadPoolExecutor(2, Runtime.getRuntime().availableProcessors(), 10,
            TimeUnit.SECONDS, new LinkedBlockingDeque<>(1024), new ThreadFactory() {
        @Override
        public Thread newThread(@org.jetbrains.annotations.NotNull Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("pool");
            return thread;
        }
    });

    /**
     * 整理发送到钉钉的信息
     *
     * @param projectName
     * @param className
     * @param methodName
     * @param requestParam
     * @param throwable
     */
    public static void pushError(@NotNull String projectName, @NotNull String className, @NotNull String methodName,
                                 String requestParam, Throwable throwable) {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                String content = "" +
                        "工程名：" + projectName + "\n" +
                        "环境：" + CommonUtil.getLocalHost() + "\n" +
                        "时间点：" + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date()) + "\n" +
                        "类名称：" + className + "\n" +
                        "参数值：" + JSONObject.toJSONString(requestParam) + "\n" +
                        "异常报警：" + CommonUtil.getStackTrace(throwable);
                content = "{\"msgtype\":\"text\",{\"content\":'" + content + "'},\"at\":{\"isAtAll\":true}}";
                String type = "application/json; charset=utf-8";
                RequestBody body = RequestBody.create(MediaType.parse(type), content);
                Request.Builder builder = new Request.Builder().url(dingUrl);
                builder.addHeader("Content-type", type).post(body);
                Request request = builder.build();
                Response response = null;
                try {
                    response = clinet.newCall(request).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (response != null) {
                        response.close();
                    }
                }
            }
        });
    }
}
